using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace GSyncConsole
{
    class Settings
    {
        public static string InvalidExtention
        {
            get { return ConfigurationManager.AppSettings["InvalidExtention"]; }
        }

        public static string ValidExtension
        {
            get { return ConfigurationManager.AppSettings["ValidExtension"]; }
        }

        public static string EmailFilter
        {
            get { return ConfigurationManager.AppSettings["EmailFilter"]; }
        }

        public static string SourceDirectory
        {
            get { return ConfigurationManager.AppSettings["SourceDirectory"]; }
        }
        public static string DestinationDirectory
        {
            get { return ConfigurationManager.AppSettings["DestinationDirectory"]; }
        }

        public static string EmailFrom
        {
            get { return ConfigurationManager.AppSettings["EmailFrom"]; }
        }

        public static string EmailTo
        {
            get { return ConfigurationManager.AppSettings["EmailTo"]; }
        }

        public static string SmtpServer
        {
            get { return ConfigurationManager.AppSettings["SmtpServer"]; }
        }

        public static int SmtpPort
        {
            get { return Convert.ToInt32(ConfigurationManager.AppSettings["SmtpPort"]); }
        }

        public static string SmtpUser
        {
            get { return ConfigurationManager.AppSettings["SmtpUser"]; }
        }       

        public static string SmtpPass
        {
            get { return ConfigurationManager.AppSettings["SmtpPass"]; }
        }

        public static bool CheckAndRunStunnelOnStartUp
        {
            get { return Convert.ToBoolean(ConfigurationManager.AppSettings["CheckAndRunStunnelOnStartUp"]); }
        }

        public static string StunnelServiceName
        {
            get 
            { 
                string serviceName = ConfigurationManager.AppSettings["StunnelServiceName"];
                if (String.IsNullOrEmpty(serviceName))
                {
                    serviceName = "stunnel";
                }
                return serviceName; 
            }
        }

        public static int SplitFileSize
        {
            get
            {
                return Convert.ToInt32(ConfigurationManager.AppSettings["SplitFileSize"]);
            }
        }

        public static bool AllowMultipleInstances
        {
            get
            {
                return Convert.ToBoolean(ConfigurationManager.AppSettings["AllowMultipleInstances"]);
            }
        }

        public static bool ScanSubDirectories
        {
            get
            {
                return Convert.ToBoolean(ConfigurationManager.AppSettings["ScanSubDirectories"]);
            }
        }

        public static bool FullDirectoryPathInSubject
        {
            get
            {
                return Convert.ToBoolean(ConfigurationManager.AppSettings["FullDirectoryPathInSubject"]);
            }
        }

        public static string CreateOrGetDataFile()
        {
            string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "GmailSyncData");
            
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            string uniqueConfigFilename = "FileStateList.bin";

            if (Settings.AllowMultipleInstances)
            {
                // current directory name(without the directory separators and drive letter)
                uniqueConfigFilename = Environment.CurrentDirectory.Replace(
                    Path.DirectorySeparatorChar.ToString(), "").Substring(2) + ".bin";

            }

            string configFilename = Path.Combine(path, uniqueConfigFilename);
                        
            return configFilename;
        }

        public static void SaveData(List<FileStateData> fileStateList)
        {
            string configFilename = CreateOrGetDataFile();

            BinaryFormatter formatter = new BinaryFormatter();
            
            using (Stream fileStream = new FileStream(configFilename, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                formatter.Serialize(fileStream, fileStateList.Count);

                foreach (FileStateData fileState in fileStateList)
                {
                    formatter.Serialize(fileStream, fileState);                    
                }

                fileStream.Close();
            }

        }

        internal static List<FileStateData> LoadData()
        {
            string configFilename = CreateOrGetDataFile();
            List<FileStateData> fileStateList = new List<FileStateData>();

            if (File.Exists(configFilename))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                using (FileStream fileStream = new FileStream(configFilename, FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    // read file count
                    int fileCount = (int)formatter.Deserialize(fileStream);

                    for (int i = 0; i < fileCount; i++)
                    {
                        FileStateData fileState = (FileStateData)formatter.Deserialize(fileStream);
                        fileStateList.Add(fileState);
                    }

                    fileStream.Close();                 
                }
            }

            return fileStateList;            
        }
    }
}
